function productExceptSelf (arr) {
  let pre = []
  let next = []
  for (let i = 0; i < arr.length; i++) {
    if (i === 0) {
      pre[i] = 1
    } else {
      pre[i] = pre[i - 1] * arr[i - 1]
    }
  }
  for (let i = arr.length - 1; i >= 0; i--) {
    if (i === arr.length - 1) {
      next[i] = 1
    } else {
      next[i] = next[i + 1] * arr[i + 1]
    }
  }
  let result = []
  for (let i = 0; i < arr.length; i++) {
    result.push(next[i] * pre[i])
  }
  return result
}
console.log(productExceptSelf([2, 3, 4, 5]))